package com.yanqu.road.dao.impl.activity.recallplayer;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.recallplayer.data.RecallPlayerBindingTaskData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BindingTaskDataDaoImpl extends TempDataObjectDao<RecallPlayerBindingTaskData> {

    public static String getBindingTaskDataSql = "select * from `t_u_recall_player_bind_task` where `userId`=?";

    public Map<Long, List<RecallPlayerBindingTaskData>> getBindingTaskData(long userId) {
        Map<Long, List<RecallPlayerBindingTaskData>> bindingTaskData = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBindingTaskDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                RecallPlayerBindingTaskData taskData = parse(rs);
                if (bindingTaskData.get(taskData.getBindUserId()) == null) {
                    bindingTaskData.put(taskData.getBindUserId(), new ArrayList<>());
                }
                bindingTaskData.get(taskData.getBindUserId()).add(taskData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return bindingTaskData;
    }

    public static String updateBindingTaskDataSql = "update `t_u_recall_player_bind_task` set taskType=?,oldValue=?,nowValue=?,gotRewarded=?,modifyTime=? where userId=? and bindUserId=? and taskId=?";

    @Override
    protected boolean updateData(RecallPlayerBindingTaskData recallPlayerBindingTaskData) {
        recallPlayerBindingTaskData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, recallPlayerBindingTaskData.getTaskType()));
        para.put(i++, new DbParameter(Types.VARCHAR, recallPlayerBindingTaskData.getOldValue().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, recallPlayerBindingTaskData.getNowValue().toString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, recallPlayerBindingTaskData.getGotRewarded()));
        para.put(i++, new DbParameter(Types.BIGINT, recallPlayerBindingTaskData.getModifyTime()));
        para.put(i++, new DbParameter(Types.BIGINT, recallPlayerBindingTaskData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, recallPlayerBindingTaskData.getBindUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, recallPlayerBindingTaskData.getTaskId()));
        if (getDb().executeNoneQuery(updateBindingTaskDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertBindingTaskDataSql = "insert into `t_u_recall_player_bind_task`(userId,bindUserId,taskId,taskType,oldValue,nowValue,gotRewarded,modifyTime) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(RecallPlayerBindingTaskData recallPlayerBindingTaskData) {
        recallPlayerBindingTaskData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, recallPlayerBindingTaskData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, recallPlayerBindingTaskData.getBindUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, recallPlayerBindingTaskData.getTaskId()));
        para.put(i++, new DbParameter(Types.INTEGER, recallPlayerBindingTaskData.getTaskType()));
        para.put(i++, new DbParameter(Types.VARCHAR, recallPlayerBindingTaskData.getOldValue().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, recallPlayerBindingTaskData.getNowValue().toString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, recallPlayerBindingTaskData.getGotRewarded()));
        para.put(i++, new DbParameter(Types.BIGINT, recallPlayerBindingTaskData.getModifyTime()));
        if (getDb().executeNoneQuery(insertBindingTaskDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected RecallPlayerBindingTaskData parseData(ResultSet rs) throws SQLException {
        RecallPlayerBindingTaskData recallPlayerBindingTaskData = new RecallPlayerBindingTaskData();
        recallPlayerBindingTaskData.setUserId(rs.getLong("userId"));
        recallPlayerBindingTaskData.setBindUserId(rs.getLong("bindUserId"));
        recallPlayerBindingTaskData.setTaskId(rs.getInt("taskId"));
        recallPlayerBindingTaskData.setTaskType(rs.getInt("taskType"));
        recallPlayerBindingTaskData.setOldValue(new BigInteger(rs.getString("oldValue")));
        recallPlayerBindingTaskData.setNowValue(new BigInteger(rs.getString("nowValue")));
        recallPlayerBindingTaskData.setGotRewarded(rs.getBoolean("gotRewarded"));
        recallPlayerBindingTaskData.setModifyTime(rs.getLong("modifyTime"));
        return recallPlayerBindingTaskData;
    }
}
